using Microsoft.Office.Interop.Excel;
using OrderSystem.SystemFramework;
using System;
using System.Data;
using System.IO;
using System.Reflection;

namespace OrderSystem.FormatOutput
{
	public class ExcelOperationYouhuaData
	{
		private _Application app;

		private _Workbook workBook;

		private _Worksheet workSheet;

		private object missing = Missing.Value;

		private string _startupPath = null;

		public ExcelOperationYouhuaData(string startupPath)
		{
			this._startupPath = startupPath;
			if (this._startupPath.Substring(this._startupPath.Length - 1, 1) != "\\")
			{
				this._startupPath += "\\";
			}
		}

		private void OpenExcel(string templetFile)
		{
			if (templetFile == null)
			{
				throw new ArgumentException("你指定的参数：templetFile 为空", "templetFile");
			}
			if (!File.Exists(templetFile))
			{
				throw new ArgumentException("你指定的参数：templetFile Excel模板文件不存在", "templetFile");
			}
			this.app = new ApplicationClass();
			this.app.Visible=true;
			this.workBook = this.app.Workbooks.Open(templetFile, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing);
			this.workSheet = (_Worksheet)this.workBook.Worksheets[1];
		}

		public void MakeYouhuaDataToExcel(string templetFile, System.Data.DataTable dtLabel, string sProjectName)
		{
			this.OpenExcel(templetFile);
			string defaultVersionOfOutType = AppSettings.GetDefaultVersionOfOutType("youhua");
			if (defaultVersionOfOutType == "1")
			{
				this.MakeYouhuaDataToExcel_1(dtLabel, sProjectName);
			}
			if (defaultVersionOfOutType == "2")
			{
				this.MakeYouhuaDataToExcel_2(dtLabel, sProjectName);
			}
			if (defaultVersionOfOutType == "3")
			{
				this.MakeYouhuaDataToExcel_3(dtLabel, sProjectName);
			}
			if (defaultVersionOfOutType == "4")
			{
				this.MakeYouhuaDataToExcel_4(dtLabel, sProjectName);
			}
			if (defaultVersionOfOutType == "5")
			{
				this.MakeYouhuaDataToExcel_5(dtLabel, sProjectName);
			}
			if (defaultVersionOfOutType == "6")
			{
				this.MakeYouhuaDataToExcel_6(dtLabel, sProjectName);
			}
			if (defaultVersionOfOutType == "7")
			{
				this.MakeYouhuaDataToExcel_7(dtLabel, sProjectName);
			}
			if (defaultVersionOfOutType == "17")
			{
				this.MakeYouhuaDataToExcel_17(dtLabel, sProjectName);
			}
		}

		private void MakeYouhuaDataToExcel_1(System.Data.DataTable dtOrderItem, string sProjectName)
		{
			int num = 2;
			try
			{
				for (int i = 0; i < dtOrderItem.Rows.Count; i++)
				{
					this.workSheet.Cells[num, 1]= (dtOrderItem.Rows[i]["标签上的客户名称"].ToString() == "") ? dtOrderItem.Rows[i]["计划名称"].ToString() : dtOrderItem.Rows[i]["标签上的客户名称"].ToString();
					this.workSheet.Cells[num, 2]= dtOrderItem.Rows[i]["产品类型"].ToString();
					this.workSheet.Cells[num, 3]= dtOrderItem.Rows[i]["产品编号"].ToString();
					this.workSheet.Cells[num, 4]= dtOrderItem.Rows[i]["宽"].ToString() + ((dtOrderItem.Rows[i]["宽(短边)"].ToString() == "") ? "" : ("/" + dtOrderItem.Rows[i]["宽(短边)"].ToString()));
					this.workSheet.Cells[num, 5]= dtOrderItem.Rows[i]["高"].ToString() + ((dtOrderItem.Rows[i]["高(短边)"].ToString() == "") ? "" : ("/" + dtOrderItem.Rows[i]["高(短边)"].ToString()));
					this.workSheet.Cells[num, 6]= dtOrderItem.Rows[i]["产品数量"].ToString();
					this.workSheet.Cells[num, 7]= dtOrderItem.Rows[i]["产品备注"].ToString() + ((dtOrderItem.Rows[i]["产品备注"].ToString() == "" || dtOrderItem.Rows[i]["产品工艺"].ToString() == "") ? "" : " / ") + dtOrderItem.Rows[i]["产品工艺"].ToString();
					this.workSheet.Cells[num, 8]= dtOrderItem.Rows[i]["产品ID"].ToString();
					num++;
				}
				this.workSheet.Columns.AutoFit();
				this.workBook.Save();
			}
			catch (Exception ex)
			{
				this.workBook.Save();
				throw ex;
			}
		}

		private void MakeYouhuaDataToExcel_2(System.Data.DataTable dtOrderItem, string sProjectName)
		{
			int num = 2;
			try
			{
				for (int i = 0; i < dtOrderItem.Rows.Count; i++)
				{
					this.workSheet.Cells[num, 1]= "N" + (i + 1).ToString() + "=";
					this.workSheet.Cells[num, 5]= (dtOrderItem.Rows[i]["标签上的客户名称"].ToString() == "") ? dtOrderItem.Rows[i]["计划名称"].ToString() : dtOrderItem.Rows[i]["标签上的客户名称"].ToString();
					this.workSheet.Cells[num, 16]= dtOrderItem.Rows[i]["产品类型"].ToString();
					this.workSheet.Cells[num, 19]= dtOrderItem.Rows[i]["产品编号"].ToString();
					this.workSheet.Cells[num, 3]= dtOrderItem.Rows[i]["宽"].ToString() + ((dtOrderItem.Rows[i]["宽(短边)"].ToString() == "") ? "" : ("/" + dtOrderItem.Rows[i]["宽(短边)"].ToString()));
					this.workSheet.Cells[num, 4]= dtOrderItem.Rows[i]["高"].ToString() + ((dtOrderItem.Rows[i]["高(短边)"].ToString() == "") ? "" : ("/" + dtOrderItem.Rows[i]["高(短边)"].ToString()));
					this.workSheet.Cells[num, 2]= dtOrderItem.Rows[i]["产品数量"].ToString();
					this.workSheet.Cells[num, 15]= dtOrderItem.Rows[i]["产品备注"].ToString() + ((dtOrderItem.Rows[i]["产品备注"].ToString() == "" || dtOrderItem.Rows[i]["产品工艺"].ToString() == "") ? "" : " / ") + dtOrderItem.Rows[i]["产品工艺"].ToString();
					this.workSheet.Cells[num, 6]= dtOrderItem.Rows[i]["产品ID"].ToString().Substring(4);
					num++;
				}
				this.workSheet.Columns.AutoFit();
				this.workBook.Save();
			}
			catch (Exception ex)
			{
				this.workBook.Save();
				throw ex;
			}
		}

		private void MakeYouhuaDataToExcel_3(System.Data.DataTable dtOrderItem, string sProjectName)
		{
			int num = 2;
			try
			{
				for (int i = 0; i < dtOrderItem.Rows.Count; i++)
				{
					this.workSheet.Cells[num, 1]= dtOrderItem.Rows[i]["宽"].ToString();
					this.workSheet.Cells[num, 2]= dtOrderItem.Rows[i]["高"].ToString();
					this.workSheet.Cells[num, 3]= dtOrderItem.Rows[i]["产品数量"].ToString();
					this.workSheet.Cells[num, 5]= (dtOrderItem.Rows[i]["标签上的客户名称"].ToString() == "") ? dtOrderItem.Rows[i]["计划名称"].ToString() : dtOrderItem.Rows[i]["标签上的客户名称"].ToString();
					this.workSheet.Cells[num, 6]= dtOrderItem.Rows[i]["产品类型"].ToString();
					this.workSheet.Cells[num, 7]= dtOrderItem.Rows[i]["产品编号"].ToString();
					this.workSheet.Cells[num, 8]= dtOrderItem.Rows[i]["产品备注"].ToString() + ((dtOrderItem.Rows[i]["产品备注"].ToString() == "" || dtOrderItem.Rows[i]["产品工艺"].ToString() == "") ? "" : " / ") + dtOrderItem.Rows[i]["产品工艺"].ToString();
					this.workSheet.Cells[num, 10]= dtOrderItem.Rows[i]["产品ID"].ToString().Substring(4);
					num++;
				}
				this.workSheet.Columns.AutoFit();
				this.workBook.Save();
			}
			catch (Exception ex)
			{
				this.workBook.Save();
				throw ex;
			}
		}

		private void MakeYouhuaDataToExcel_4(System.Data.DataTable dtOrderItem, string sProjectName)
		{
			int num = 2;
			try
			{
				for (int i = 0; i < dtOrderItem.Rows.Count; i++)
				{
					this.workSheet.Cells[num, 1]= dtOrderItem.Rows[i]["宽"].ToString();
					this.workSheet.Cells[num, 2]= dtOrderItem.Rows[i]["高"].ToString();
					this.workSheet.Cells[num, 3]= dtOrderItem.Rows[i]["产品数量"].ToString();
					this.workSheet.Cells[num, 4]= (dtOrderItem.Rows[i]["标签上的客户名称"].ToString() == "") ? dtOrderItem.Rows[i]["计划名称"].ToString() : dtOrderItem.Rows[i]["标签上的客户名称"].ToString();
					this.workSheet.Cells[num, 5]= string.Concat(new string[]
					{
						dtOrderItem.Rows[i]["产品类型"].ToString(),
						" ",
						dtOrderItem.Rows[i]["产品数量"].ToString(),
						dtOrderItem.Rows[i]["类型单位"].ToString(),
						" -",
						dtOrderItem.Rows[i]["产品编号"].ToString()
					});
					num++;
				}
				this.workSheet.Columns.AutoFit();
				this.workBook.Save();
			}
			catch (Exception ex)
			{
				this.workBook.Save();
				throw ex;
			}
		}

		private void MakeYouhuaDataToExcel_5(System.Data.DataTable dtOrderItem, string sProjectName)
		{
			int num = 2;
			try
			{
				for (int i = 0; i < dtOrderItem.Rows.Count; i++)
				{
					this.workSheet.Cells[num, 1]= dtOrderItem.Rows[i]["产品数量"].ToString();
					this.workSheet.Cells[num, 2]= dtOrderItem.Rows[i]["宽"].ToString();
					this.workSheet.Cells[num, 3]= dtOrderItem.Rows[i]["高"].ToString();
					this.workSheet.Cells[num, 4]= (dtOrderItem.Rows[i]["标签上的客户名称"].ToString() == "") ? dtOrderItem.Rows[i]["计划名称"].ToString() : dtOrderItem.Rows[i]["标签上的客户名称"].ToString();
					this.workSheet.Cells[num, 5]= dtOrderItem.Rows[i]["产品ID"].ToString().Substring(4);
					this.workSheet.Cells[num, 6]= "-";
					this.workSheet.Cells[num, 7]= "";
					this.workSheet.Cells[num, 8]= dtOrderItem.Rows[i]["产品类型"].ToString();
					this.workSheet.Cells[num, 10]= dtOrderItem.Rows[i]["产品备注"].ToString() + ((dtOrderItem.Rows[i]["产品备注"].ToString() == "" || dtOrderItem.Rows[i]["产品工艺"].ToString() == "") ? "" : " / ") + dtOrderItem.Rows[i]["产品工艺"].ToString();
					this.workSheet.Cells[num, 11]= dtOrderItem.Rows[i]["产品编号"].ToString();
					this.workSheet.Cells[num, 12]= dtOrderItem.Rows[i]["产品ID"].ToString().Substring(4).Split(new char[]
					{
						'-'
					}).GetValue(1).ToString();
					num++;
				}
				this.workSheet.Columns.AutoFit();
				this.workBook.Save();
			}
			catch (Exception ex)
			{
				this.workBook.Save();
				throw ex;
			}
		}

		private void MakeYouhuaDataToExcel_6(System.Data.DataTable dtOrderItem, string sProjectName)
		{
			int num = 2;
			try
			{
				for (int i = 0; i < dtOrderItem.Rows.Count; i++)
				{
					this.workSheet.Cells[num, 1]= ((dtOrderItem.Rows[i]["标签上的客户名称"].ToString() == "") ? dtOrderItem.Rows[i]["计划名称"].ToString() : dtOrderItem.Rows[i]["标签上的客户名称"].ToString()) + " " + dtOrderItem.Rows[i]["产品ID"].ToString().Substring(4);
					this.workSheet.Cells[num, 2]= dtOrderItem.Rows[i]["产品类型"].ToString();
					this.workSheet.Cells[num, 3]= dtOrderItem.Rows[i]["产品编号"].ToString();
					this.workSheet.Cells[num, 4]= dtOrderItem.Rows[i]["宽"].ToString();
					this.workSheet.Cells[num, 5]= dtOrderItem.Rows[i]["高"].ToString();
					this.workSheet.Cells[num, 6]= dtOrderItem.Rows[i]["产品数量"].ToString();
					this.workSheet.Cells[num, 7]= dtOrderItem.Rows[i]["产品备注"].ToString() + ((dtOrderItem.Rows[i]["产品备注"].ToString() == "" || dtOrderItem.Rows[i]["产品工艺"].ToString() == "") ? "" : " / ") + dtOrderItem.Rows[i]["产品工艺"].ToString();
					string text = string.Concat(new string[]
					{
						dtOrderItem.Rows[i]["宽"].ToString(),
						(dtOrderItem.Rows[i]["宽(短边)"].ToString() == "") ? "" : ("/" + dtOrderItem.Rows[i]["宽(短边)"].ToString()),
						"×",
						dtOrderItem.Rows[i]["高"].ToString(),
						(dtOrderItem.Rows[i]["高(短边)"].ToString() == "") ? "" : ("/" + dtOrderItem.Rows[i]["高(短边)"].ToString())
					});
					this.workSheet.Cells[num, 8]= text;
					this.workSheet.Cells[num, 10]= "0";
					this.workSheet.Cells[num, 11]= text;
					num++;
				}
				this.workSheet.Columns.AutoFit();
				this.workBook.Save();
			}
			catch (Exception ex)
			{
				this.workBook.Save();
				throw ex;
			}
		}

		private void MakeYouhuaDataToExcel_7(System.Data.DataTable dtLabel, string sProjectName)
		{
			int num = 2;
			try
			{
				for (int i = 0; i < dtLabel.Rows.Count; i++)
				{
					this.workSheet.Cells[num, 1]= (i + 1).ToString();
					this.workSheet.Cells[num, 2]= dtLabel.Rows[i]["产品ID"].ToString().Substring(4);
					this.workSheet.Cells[num, 3]= dtLabel.Rows[i]["产品编号"].ToString();
					this.workSheet.Cells[num, 4]= dtLabel.Rows[i]["宽"].ToString();
					this.workSheet.Cells[num, 5]= dtLabel.Rows[i]["高"].ToString();
					this.workSheet.Cells[num, 6]= dtLabel.Rows[i]["产品数量"].ToString();
					this.workSheet.Cells[num, 7]= "0";
					this.workSheet.Cells[num, 8]= "0";
					this.workSheet.Cells[num, 9]= (dtLabel.Rows[i]["标签上的客户名称"].ToString() == "") ? dtLabel.Rows[i]["计划名称"].ToString() : dtLabel.Rows[i]["标签上的客户名称"].ToString();
					this.workSheet.Cells[num, 10]= dtLabel.Rows[i]["产品备注"].ToString() + ((dtLabel.Rows[i]["产品备注"].ToString() == "" || dtLabel.Rows[i]["产品工艺"].ToString() == "") ? "" : " / ") + dtLabel.Rows[i]["产品工艺"].ToString();
					this.workSheet.Cells[num, 11]= dtLabel.Rows[i]["产品类型"].ToString();
					this.workSheet.Cells[num, 12]= "否";
					num++;
				}
				this.workSheet.Columns.AutoFit();
				this.workBook.Save();
			}
			catch (Exception ex)
			{
				this.workBook.Save();
				throw ex;
			}
		}

		private void MakeYouhuaDataToExcel_17(System.Data.DataTable dtOrderItem, string sProjectName)
		{
			int num = 3;
			try
			{
				for (int i = 0; i < dtOrderItem.Rows.Count; i++)
				{
					this.workSheet.Cells[num, 1]= (dtOrderItem.Rows[i]["标签上的客户名称"].ToString() == "") ? dtOrderItem.Rows[i]["计划名称"].ToString() : dtOrderItem.Rows[i]["标签上的客户名称"].ToString();
					this.workSheet.Cells[num, 2]= dtOrderItem.Rows[i]["产品数量"].ToString();
					this.workSheet.Cells[num, 4]= dtOrderItem.Rows[i]["宽"].ToString();
					this.workSheet.Cells[num, 5]= dtOrderItem.Rows[i]["高"].ToString();
					this.workSheet.Cells[num, 6]= dtOrderItem.Rows[i]["产品ID"].ToString().Substring(4);
					this.workSheet.Cells[num, 7]= dtOrderItem.Rows[i]["产品编号"].ToString();
					this.workSheet.Cells[num, 11]= dtOrderItem.Rows[i]["产品类型"].ToString();
					this.workSheet.Cells[num, 12]= dtOrderItem.Rows[i]["产品备注"].ToString() + ((dtOrderItem.Rows[i]["产品备注"].ToString() == "" || dtOrderItem.Rows[i]["产品工艺"].ToString() == "") ? "" : " / ") + dtOrderItem.Rows[i]["产品工艺"].ToString();
					num++;
				}
				this.workBook.Save();
			}
			catch (Exception ex)
			{
				this.workBook.Save();
				throw ex;
			}
		}
	}
}
